//
//  ZCPicturesView.swift
//  weibo
//
//  Created by 张城 on 16/7/25.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit
import SnapKit



let cellWH: CGFloat = 80
let cellMargin: CGFloat = 5
let cellId: String = "cell"


class ZCPicturesView: UICollectionView {
    
    var pictures: [ZCPicturesModel]?{
        didSet{
            
            self.numLabel.text = String(pictures?.count ?? 0)
            
            let collectionViewSize = getCVSize((pictures?.count)!)
            
            self.snp_updateConstraints { (make) in
                make.size.equalTo(collectionViewSize)
            }
            
            reloadData()
            
            
        }
    }
    
    func getCVSize(count: Int) -> CGSize {
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row = count == 4 ? 2 : (count-1)/3+1
         
        let W = CGFloat(col) * cellWH + CGFloat(col-1)*cellMargin
        let H = CGFloat(row) * cellWH + CGFloat(row-1)*cellMargin
        
        return CGSizeMake(W, H)
        
    }
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupPictures()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupPictures() {
        backgroundColor = UIColor.clearColor()
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(cellWH, cellWH)
        layout.minimumLineSpacing = cellMargin
        layout.minimumInteritemSpacing = cellMargin
        dataSource = self
        
        self.registerClass(ZCPicturesCell.self, forCellWithReuseIdentifier: cellId)
        
        addSubview(numLabel)
        
        numLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
    }
    
    
    private lazy var numLabel: UILabel = UILabel(textColor: UIColor.greenColor(), textFont: 20)
    

}


extension ZCPicturesView : UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (pictures?.count) ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! ZCPicturesCell
        cell.picture = pictures![indexPath.item]
        return cell
    }
    
}


class ZCPicturesCell: UICollectionViewCell {
    
    var picture: ZCPicturesModel? {
        didSet{
            
            picImg.zc_setImg((picture?.thumbnail_pic)!, holdImgName: "weibo_placeholder")
            
            if let picTh = picture?.thumbnail_pic where picTh.hasSuffix("gif") {
                self.gifImg.hidden = false
            }else{
                self.gifImg.hidden = true
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor.redColor()
        setupImg()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupImg() {
        addSubview(picImg)
        addSubview(gifImg)
        
        picImg.snp_makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        
        gifImg.snp_makeConstraints { (make) in
            make.trailing.equalTo(contentView.snp_trailing)
            make.bottom.equalTo(contentView.snp_bottom)
        }
    }
    
    private lazy var picImg: UIImageView = {
        let pic = UIImageView(image: UIImage(named: "weibo_placeholder"))
        pic.contentMode = .ScaleAspectFill
        pic.layer.masksToBounds = true
        return pic
    }()
   
    private lazy var gifImg: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
}



